﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{
    public class KhuVucDAO
    {
        public static DataTable DanhSachKhuVuc()
        {
            SqlConnection conn = DataProvider.Connection();
            string str = "usp_DanhSachKhuVuc";
            SqlDataAdapter data = new SqlDataAdapter(str, conn);
            DataTable dt = new DataTable();
            data.Fill(dt);
            return dt;
        }

        public static int KiemTraKhuVuc(string ten)
        {
            SqlConnection conn = DataProvider.Connection();
            conn.Open();
            string sql = "usp_KiemTraKhuVuc";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@TenKV", SqlDbType.NVarChar).Value = ten;
            cmd.Parameters.Add("@k", SqlDbType.Int);
            cmd.Parameters["@k"].Direction = ParameterDirection.Output;
            cmd.ExecuteNonQuery();
            int k = Int32.Parse(cmd.Parameters["@k"].Value.ToString());
            if (k == 1)
                return 1;
            else
                return 0;
        }

        public static int CapNhatKhuVuc(KhuVucDTO kv)
        {
            SqlConnection conn = DataProvider.Connection();
            conn.Open();
            string str = "usp_CapNhatKhuVuc";
            SqlCommand cmd = new SqlCommand(str, conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@MaKV", SqlDbType.Int).Value = kv.MaKhuVuc;
            cmd.Parameters.Add("@TenKV", SqlDbType.NVarChar).Value = kv.TenKhuVuc;
            cmd.Parameters.Add("@GioNang", SqlDbType.Float).Value = kv.SoGioNang;
            int kq = cmd.ExecuteNonQuery();
            conn.Close();
            if (kq >= 0)
                return 1;
            else
                return 0;
        }

        public static int ThemKhuVuc(KhuVucDTO kv)
        {
            SqlConnection conn = DataProvider.Connection();
            conn.Open();
            string sql = "usp_ThemKhuVuc";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@TenKV", SqlDbType.NVarChar).Value = kv.TenKhuVuc;
            cmd.Parameters.Add("@SoGioNang", SqlDbType.Float).Value = kv.SoGioNang;
            int kq = cmd.ExecuteNonQuery();
            conn.Close();
            if (kq >= 0)
                return 1;
            else
                return 0;
        }
    }
}
